package MyComponent// ActionScript file
{
	import fl.core.InvalidationType;
	import fl.core.UIComponent;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.utils.getDefinitionByName;

	public class MyPercentBar extends UIComponent
	{
		private var _background:DisplayObject;
		private var _thumb:DisplayObject;
		private var _runMan:DisplayObject;
		private var _backgroundSP:Sprite;
		private var _thumbSP:Sprite;
		private var _excursion_x:Number;
		private var _excursion_y:Number;
		private var _nowPercent:int=100;
		private static var defaultStyles:Object={backgroundSkin:"MyNumberBar_default_backgroundSkin",thumbSkin:"MyNumberBar_default_thumbSkin",runManSkin:"null"};
		public function MyPercentBar():void
		{
			this.init();
			super();
		}
		
		private function init():void
		{
			this._backgroundSP=new Sprite;
			this._thumbSP=new Sprite;
			this.addChild(this.backgroundSP);
			this.addChild(this.thumbSP);
		}
		
		public static function getStyleDefinition():Object
		{
			return defaultStyles;
		}
		
		override protected function draw():void
		{
			if (this.isInvalid(InvalidationType.STYLES))
			{
				this.drawMyBar();
				this.invalidate(InvalidationType.SIZE,false);
				this.invalidate(InvalidationType.STATE,false);
			}
			if (this.isInvalid(InvalidationType.SIZE)) 
			{
				this.drawLayout();
			}
			if (this.isInvalid(InvalidationType.STATE)) 
			{
				this.setState();
			}
			super.draw();
		}
		
		protected function drawMyBar():void
		{
			if(this.background)this.backgroundSP.removeChild(this.background);
			if(this.thumb)
			{
				this.thumbSP.removeChild(this.thumb);
				this.thumbSP.removeChild(this.thumb.mask);
				this.thumb.mask=null;
			}
			if(this.runMan)
			{
				this.thumbSP.removeChild(this.runMan);
				this.runMan=null;
			}
			this.background=this.getDisplayObjectInstance(this.getStyleValue("backgroundSkin"));
			this.thumb=this.getDisplayObjectInstance(this.getStyleValue("thumbSkin"));
			this.thumb.mask=this.getDisplayObjectInstance(this.getStyleValue("thumbSkin"));
			this.backgroundSP.addChild(this.background);
			this.thumbSP.addChild(this.thumb);
			this.thumbSP.addChild(this.thumb.mask);
			var runManValue:String=String(this.getStyleValue("runManSkin"));
			if(runManValue!="null")
			{
				this.runMan=this.getDisplayObjectInstance(runManValue);
				this.thumbSP.addChild(this.runMan);
			}
		}
		
		protected function drawLayout():void
		{
			this.background.width=this.width;
			this.background.height=this.height;
			this.thumb.width=this.width-2;
			this.thumb.height=this.height-2;
			this.thumb.mask.width=this.width-2;
			this.thumb.mask.height=this.height-2;
			this.thumbSP.x=this.excursion_x;
			this.thumbSP.y=this.excursion_y;
		}
		
		protected function setState():void
		{
			var setX:Number=-this.thumb.width*(1-this.nowPercent/100);
			this.thumb.x=setX;
			if(this.runMan)this.runMan.x=setX+this.thumb.width;
		}
		
		public function get background():DisplayObject
		{
			return _background;
		}

		public function set background(value:DisplayObject):void
		{
			_background = value;
		}

		public function get thumb():DisplayObject
		{
			return _thumb;
		}

		public function set thumb(value:DisplayObject):void
		{
			_thumb = value;
		}
		
		[Inspectable(defaultValue="MyNumberBar_default_backgroundSkin",type="String")]
		public function set backgroundSkin(value:String):void//背景
		{
			if(value==this.getStyleValue("backgroundSkin"))return;
			try
			{
				var targetClass:Object=getDefinitionByName(value);
				this.setStyle("backgroundSkin",value);
			}
			catch(e:Error)
			{
				trace("没有资源")
			}
		}
		
		[Inspectable(defaultValue="MyNumberBar_default_thumbSkin",type="String")]
		public function set thumbSkin(value:String):void//滑动槽
		{
			if(value==this.getStyleValue("thumbSkin"))return;
			try
			{
				var targetClass:Object=getDefinitionByName(value);
				this.setStyle("thumbSkin",value);
			}
			catch(e:Error)
			{
				trace("没有资源")
			}
		}
		
		[Inspectable(defaultValue="null",type="String")]
		public function set runManSkin(value:String):void
		{
			if(value==this.getStyleValue("runManSkin"))return;
			try
			{
				if(value!="null")
				{
					var targetClass:Object=getDefinitionByName(value);
				}
				this.setStyle("runManSkin",value);
			}
			catch(e:Error)
			{
				trace("没有资源")
			}
		}

		[Inspectable(defaultValue=0,type="Number")]
		public function get excursion_x():Number
		{
			return _excursion_x;
		}

		public function set excursion_x(value:Number):void
		{
			if(value==this.excursion_x)return;
			_excursion_x = value;
			this.invalidate(InvalidationType.SIZE);
		}

		[Inspectable(defaultValue=0,type="Number")]
		public function get excursion_y():Number
		{
			return _excursion_y;
		}

		public function set excursion_y(value:Number):void
		{
			if(value==this.excursion_y)return;
			_excursion_y = value;
			this.invalidate(InvalidationType.SIZE);
		}

		public function get thumbSP():Sprite
		{
			return _thumbSP;
		}

		public function get backgroundSP():Sprite
		{
			return _backgroundSP;
		}

		public function get nowPercent():int
		{
			return _nowPercent;
		}

		public function set nowPercent(value:int):void
		{
			if(value==this.nowPercent)return;
			if(value<0)
			{
				value=0;
			}
			else if(value>100)
			{
				value=100;
			}
			_nowPercent = value;
			this.invalidate(InvalidationType.STATE);
		}

		public function get runMan():DisplayObject
		{
			return _runMan;
		}

		public function set runMan(value:DisplayObject):void
		{
			_runMan = value;
		}


	}
}